package com.apes.scm.masterdata.frame.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.express.FuncAlias;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.frame.model.DutyCenter;
import com.apes.scm.masterdata.frame.repository.DutyCenterRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 功能：责任中心 Service
 *
 * @author HuJianlin
 * @create 2018-03-08 14:35
 */
@Service("dutyCenterService")
public class DutyCenterService extends DomainService {

    @Autowired
    DutyCenterRepository dutyCenterRepository;

    public DutyCenter save(SimpleRequest request) {
        DutyCenter dutyCenter = request.getO(DutyCenter.class);
        return dutyCenterRepository.saveAndFlush(dutyCenter);
    }

    /**
     * 功能：查询
     *
     * @param request
     */
    public Page<DutyCenter> findAll(SimpleRequest request) {
        List<Map> filters = request.get("condition");
        return dutyCenterRepository.findAll(JpaDsl.toCriteria(filters),PageRequest.of(request.get("page"), request.get("size")));
    }

    /**
     * 下拉插叙
     * @param request
     * @return
     * @throws Exception
     */
    public Page<DutyCenter> find(SimpleRequest request) throws Exception{
        return dutyCenterRepository.find( request.get("key"), PageRequest.of(request.get("page"), request.get("size")) );
    }


    /**
     * 下拉插叙
     * @param request
     * @return
     * @throws Exception
     */
    public Page<DutyCenter> findByLocalPermit(SimpleRequest request) throws Exception{
        String postId = request.getPostId();
        if(postId == null) throw new RuntimeException("未找到当前人员的登录信息，请重新登录！");
        List<String> stringList = dutyCenterRepository.findLocalPermitOrgId(postId,"pp_belong","pt_dept");
        if (!stringList.isEmpty()){
            return dutyCenterRepository.findDeptIdList(stringList,PageRequest.of(request.get("page"), request.get("size")));
        }
        return dutyCenterRepository.find( request.get("key"), PageRequest.of(request.get("page"), request.get("size")) );
    }

    /**
     * 删除
     * @param request
     * @return
     * @throws Exception
     */
    public String delete(SimpleRequest request) throws Exception  {
        Integer versions = request.get("version");
        DutyCenter dutyCenter = request.getO(DutyCenter.class);
        if (dutyCenter.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        dutyCenterRepository.delete(dutyCenter);
        return "删除成功";


    }

    /**
     * 通过部门获取利润中心
     */
    @FuncAlias(name = "getDutyCenterById")
    public DutyCenter getDutyCenterById(String id) {
        DutyCenter dutyCenter =dutyCenterRepository.findOne(id);
        if (dutyCenter == null) throw new RuntimeException(" 没有定义责任中心！");
        return dutyCenter;
    }


}
